package com.springboot.service;

import com.springboot.dao.ProductInfoRespository;
import com.springboot.dto.CartDTO;
import com.springboot.enums.ProductStatusEnum;
import com.springboot.enums.ResultEnum;
import com.springboot.exception.SellException;
import com.springboot.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductInfoRespository productInfoRespository;


    @Override
    public ProductInfo findOne(String productId) {
        return productInfoRespository.findOne(productId);
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoRespository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoRespository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = productInfoRespository.findOne(cartDTO.getProductId());
            if (productInfo == null)
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            productInfoRespository.save(productInfo);

        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = productInfoRespository.findOne(cartDTO.getProductId());
            if (productInfo == null)
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result <= 0)
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            productInfo.setProductStock(result);
            this.save(productInfo);
        }

    }


    @Override
    public List<ProductInfo> findAll() {
        return productInfoRespository.findAll();
    }


    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = productInfoRespository.findOne(productId);
        if (productInfo == null)
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        if (productInfo.getProductStatus() == ProductStatusEnum.UP.getCode())
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        //更新
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return productInfoRespository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = productInfoRespository.findOne(productId);
        if (productInfo == null)
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        if (productInfo.getProductStatus() == ProductStatusEnum.DOWN.getCode())
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        //更新
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productInfoRespository.save(productInfo);
    }
}
